package net.sourceforge.m2settings.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import net.sourceforge.m2settings.SettingsXMLFileBinder;
import net.sourceforge.m2settings.gui.listerners.ClickSaveActionListener;
import net.sourceforge.m2settings.model.MirrorTableModel;
import net.sourceforge.m2settings.model.ProxyTableModel;
import net.sourceforge.m2settings.model.ServerTableModel;

import org.apache.log4j.Logger;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.ValidationException;

public class MainWindow extends JFrame {
	private static final long serialVersionUID = -2563430435503008693L;

	private JPanel jContentPane = null;

	private JTabbedPane jTabbedPane = null;

	private JPanel jPanel = null;

	private JTextField jTextField = null;

	private JLabel jLabel = null;

	private JLabel jLabel1 = null;

	private JCheckBox jCheckBox = null;

	private JLabel jLabel2 = null;

	private JCheckBox jCheckBox1 = null;

	private JLabel jLabel3 = null;

	private JCheckBox jCheckBox2 = null;

	private JPanel jPanelProxies = null;
	private JPanel jPanelMirrors = null;
	private JPanel jPanelServers = null;
	
	private Logger log = Logger.getLogger(MainWindow.class);  //  @jve:decl-index=0:

	private JMenuBar jJMenuBar = null;

	private JMenu jMenuFile = null;

	private JMenuItem jMenuItemFileSave = null;

	private JMenuItem jMenuItemFileExit = null;

	private SettingsXMLFileBinder xmlbind;


	/**
	 * This method initializes jTabbedPane	
	 * 	
	 * @return javax.swing.JTabbedPane	
	 */
	private JTabbedPane getJTabbedPane() {
		if (jTabbedPane == null) {
			jTabbedPane = new JTabbedPane();
			jTabbedPane.addTab("General", null, getJPanel(), null);
			jTabbedPane.addTab("Proxies", null, getJPanelProxies(), "<html>Configuration for different proxy profiles. <br>Multiple proxy profiles might come in handy for anyone working from a notebook or other <br>mobile platform, to enable easy switching of entire proxy configurations by simply <br/>specifying the profile id, again either from the command line or from the <br>defaults section below.</html>");
			jTabbedPane.addTab("Mirrors", null, getJPanelMirrors(), "Configuration of download mirrors for repositories. ");
			jTabbedPane.addTab("Servers", null, getJPanelServers(), "<html>Configuration of server-specific settings, mainly authentication method. <br>This allows configuration of authentication on a per-server basis.</html>");
		}
		return jTabbedPane;
	}



	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel3 = new JLabel();
			jLabel3.setText("offline");
			jLabel2 = new JLabel();
			jLabel2.setText("usePluginRegistry");
			jLabel2.setToolTipText("");
			jLabel1 = new JLabel();
			jLabel1.setText("interactiveMode");
			jLabel1.setToolTipText("");
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(4);
			gridLayout.setColumns(2);
			jLabel = new JLabel();
			jLabel.setText("localRepository");
			jLabel.setToolTipText("");
			jPanel = new JPanel();
			jPanel.setLayout(gridLayout);
			jPanel.add(jLabel, null);
			jPanel.add(getJTextField(), null);
			jPanel.add(jLabel1, null);
			jPanel.add(getJCheckBox(), null);
			jPanel.add(jLabel2, null);
			jPanel.add(getJCheckBox1(), null);
			jPanel.add(jLabel3, null);
			jPanel.add(getJCheckBox2(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setText("...");
			jTextField.setToolTipText("The local repository.");
		}
		return jTextField;
	}

	/**
	 * This method initializes jCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBox() {
		if (jCheckBox == null) {
			jCheckBox = new JCheckBox();
			jCheckBox.setToolTipText("Whether Maven should attempt to interact with the user for input.");
			jCheckBox.setText("not interactive");
		}
		return jCheckBox;
	}

	/**
	 * This method initializes jCheckBox1	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBox1() {
		if (jCheckBox1 == null) {
			jCheckBox1 = new JCheckBox();
			jCheckBox1.setToolTipText("Whether Maven should use the plugin-registry.xml file to manage plugin versions.");
			jCheckBox1.setText("don't use plugin-registry.xml");
		}
		return jCheckBox1;
	}

	/**
	 * This method initializes jCheckBox2	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBox2() {
		if (jCheckBox2 == null) {
			jCheckBox2 = new JCheckBox();
			jCheckBox2.setText("online");
			jCheckBox2.setToolTipText("Indicate whether maven should operate in offline mode full-time.");
		}
		return jCheckBox2;
	}

	/**
	 * This method initializes jPanelProxies	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelProxies() {
		if (jPanelProxies == null) {
			ProxyTableModel proxyTableModel=new ProxyTableModel(xmlbind.getSettings());
			jPanelProxies = new JTablePanel(proxyTableModel);
		}
		return jPanelProxies;
	}
	/**
	 * This method initializes jPanelMirrors
	 * @return 
	 */
	private Component getJPanelMirrors() {
		if (jPanelMirrors == null) {
			MirrorTableModel mirrorTableModel=new MirrorTableModel(xmlbind.getSettings());
			jPanelMirrors = new JTablePanel(mirrorTableModel);
		}
		return jPanelMirrors;
	}

	/**
	 * This method initializes jPanelServers	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelServers() {
		if (jPanelServers == null) {
			ServerTableModel serverTableMode=new ServerTableModel(xmlbind.getSettings());
			jPanelServers=new JTablePanel(serverTableMode);
		}
		return jPanelServers;
	}

	
	/**
	 * This method initializes jJMenuBar	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getJMenuFile());
		}
		return jJMenuBar;
	}



	/**
	 * This method initializes jMenuFile	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getJMenuFile() {
		if (jMenuFile == null) {
			jMenuFile = new JMenu();
			jMenuFile.setText("File");
			jMenuFile.add(getJMenuItemFileSave());
			jMenuFile.add(getJMenuItemFileExit());
		}
		return jMenuFile;
	}



	/**
	 * This method initializes jMenuItemFileSave	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemFileSave() {
		if (jMenuItemFileSave == null) {
			jMenuItemFileSave = new JMenuItem();
			jMenuItemFileSave.setText("Save");
			ActionListener clickSaveActionListener=new ClickSaveActionListener(xmlbind);
			jMenuItemFileSave.addActionListener(clickSaveActionListener);
		}
		return jMenuItemFileSave;
	}



	/**
	 * This method initializes jMenuItemFileExit	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemFileExit() {
		if (jMenuItemFileExit == null) {
			jMenuItemFileExit = new JMenuItem();
			jMenuItemFileExit.setText("Exit");
			jMenuItemFileExit.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					//TODO confirm dialog
					System.exit(0);
				}
			});
		}
		return jMenuItemFileExit;
	}



	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				try {
					File f = new File(System.getProperty("user.home")+"/.m2/settings.xml");

					MainWindow thisClass = new MainWindow(f);
					thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					thisClass.setVisible(true);
				} catch (MarshalException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ValidationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				

			}
		});
	}

	/**
	 * This is the default constructor
	 * @throws ValidationException 
	 * @throws MarshalException 
	 */
	public MainWindow(File settingsFile) throws MarshalException, ValidationException {
		super();
		log.debug("welcome $Id$");
		try {
			xmlbind=new SettingsXMLFileBinder(settingsFile);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			xmlbind.loadSettings();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		initialize();
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setJMenuBar(getJJMenuBar());
		this.setName("main");
		this.setContentPane(getJContentPane());
		this.setTitle("m2settings");
		
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJTabbedPane(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	
}
